﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using Autofac.Builder;
using Autofac.Features.Scanning;

namespace BasicCMS
{
    /// <summary>
    /// 对Autofac中的ContainerBuidler的扩展
    /// </summary>
    public static class ContainerBuilderExtention
    {
        /// <summary>
        /// 注册程序集
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="assemblies"></param>
        /// <returns></returns>
        public static IEnumerable<IRegistrationBuilder<object, ScanningActivatorData, DynamicRegistrationStyle>> RegisterAssmelbies(this ContainerBuilder builder, params Assembly[] assemblies)
        {
            return new IRegistrationBuilder<object, Autofac.Features.Scanning.ScanningActivatorData, DynamicRegistrationStyle>[]{
                builder.RegisterAssemblyTypes(assemblies).Where(m=>ComponentRegistryAttribute.ValidateType(m,Lifetime.Transient)).AsSelf().AsImplementedInterfaces(), //瞬态
                builder.RegisterAssemblyTypes(assemblies).Where(m=>ComponentRegistryAttribute.ValidateType(m,Lifetime.Singleton)).AsSelf().AsImplementedInterfaces().SingleInstance(), //单例
                builder.RegisterAssemblyTypes(assemblies).Where(m=>ComponentRegistryAttribute.ValidateType(m,Lifetime.Container)).AsSelf().AsImplementedInterfaces().InstancePerLifetimeScope(), //容器
            };
        }
    }
}
